package com.example.campusproject.adapter;

import com.example.campusproject.ResourceTable;
import com.example.campusproject.entity.YWNN;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class YWNNListAdapter extends BaseItemProvider {
    private AbilitySlice context;
    private List<YWNN> ywnns;

    public YWNNListAdapter(AbilitySlice context, List<YWNN> ywnns) {
        this.context = context;
        this.ywnns = ywnns;
    }

    @Override
    public int getCount() {
        return ywnns.size();
    }

    @Override
    public Object getItem(int i) {
        return ywnns.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class Holder{
        Image image;
        Text name,info;

        public Holder(Component component) {
            image= (Image) component.findComponentById(ResourceTable.Id_iv_ywnn_image);
            name= (Text) component.findComponentById(ResourceTable.Id_tv_ywnn_name);
            info= (Text) component.findComponentById(ResourceTable.Id_tv_ywnn_info);
        }
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        //定义布局对象
        Component pc;
        //定义Holder对象
        Holder holder;
        //判断缓存布局是否为空
        if (component==null){
            //缓存为空，说明未加载过列表，需要手动加载子布局
            pc=LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ability_ywnn_item,null,false);
            holder=new Holder(pc);
            pc.setTag(holder);
        }else {
            //已经缓存过，则不需要重复加载，直接使用缓存布局即可
            pc=component;
            holder= (Holder) pc.getTag();
        }
        holder.name.setText(ywnns.get(i).getName());
        holder.info.setText(ywnns.get(i).getInfo());
        holder.image.setPixelMap(ywnns.get(i).getImage());
        return pc;
    }
}
